<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名字" prop="name">
      <el-input v-model="dataForm.name" placeholder="名字"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="等级" prop="level">
      <el-input v-model="dataForm.level" placeholder="等级"></el-input>
    </el-form-item>
    <el-form-item label="父目录id" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="父目录id"></el-input>
    </el-form-item>
    <el-form-item label="宿舍人数" prop="count">
      <el-input v-model="dataForm.count" placeholder="宿舍人数"></el-input>
    </el-form-item>
    <el-form-item label="宿舍学生性别(0:男 1：女)" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="宿舍学生性别(0:男 1：女)"></el-input>
    </el-form-item>
    <el-form-item label="年级（0：大一 1：大二 2：大三 3：大四）" prop="grade">
      <el-input v-model="dataForm.grade" placeholder="年级（0：大一 1：大二 2：大三 3：大四）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          createTime: '',
          level: '',
          parentId: '',
          count: '',
          sex: '',
          grade: ''
        },
        dataRule: {
          name: [
            { required: true, message: '名字不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          level: [
            { required: true, message: '等级不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '父目录id不能为空', trigger: 'blur' }
          ],
          count: [
            { required: true, message: '宿舍人数不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '宿舍学生性别(0:男 1：女)不能为空', trigger: 'blur' }
          ],
          grade: [
            { required: true, message: '年级（0：大一 1：大二 2：大三 3：大四）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/dormarea/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.dormArea.name
                this.dataForm.createTime = data.dormArea.createTime
                this.dataForm.level = data.dormArea.level
                this.dataForm.parentId = data.dormArea.parentId
                this.dataForm.count = data.dormArea.count
                this.dataForm.sex = data.dormArea.sex
                this.dataForm.grade = data.dormArea.grade
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/dormarea/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'createTime': this.dataForm.createTime,
                'level': this.dataForm.level,
                'parentId': this.dataForm.parentId,
                'count': this.dataForm.count,
                'sex': this.dataForm.sex,
                'grade': this.dataForm.grade
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
